using System;
using System.Xml;
using Google.Protobuf;
using HuaXianQu.ProtoBuffEx.Runtime;
using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEngine;

namespace HuaXianQu.ProtoBuffEx.ToolEditor
{
    [CustomEditor(typeof(ProtoDataImporter))]
    public class ProtoDatImporterEditor : ScriptedImporterEditor
    {
        private Vector2 scrollPosition;

        GUIStyle style = new GUIStyle()
        {
        };

        float lineHeight;
        int lineCount = 0;
        float _height;

        private float height
        {
            get => _height;
            set => _height = Mathf.Clamp(value, 300, 1000);
        }

        // ProtoData;
        string protoTypeStr;
        private uint hash;
        string protoJsonStr;
        bool isTaskCompleted = true;

        public override void OnEnable()
        {
            base.OnEnable();
            InitData();
        }

        public override bool showImportedObject => false;

        private void InitData()
        {
            MessageSerializer protoDataImporter = (MessageSerializer)assetTarget;

            style.normal.textColor = Color.black;
            style.normal.background = EditorGUIUtility.whiteTexture; // 设置背景颜色
            protoTypeStr = protoDataImporter.ProtoType;

            byte[] data = protoDataImporter.ProtoData;
            if (data == null)
            {
                return;
            }

            protoJsonStr = GetProtoBuffStatic(protoTypeStr, data);
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            if (GUILayout.Button("搜索"))
            {
                lineHeight = EditorStyles.textArea.lineHeight;
                lineCount++;
                scrollPosition = Vector2.up * lineHeight * lineCount;
            }

            height = EditorGUILayout.FloatField("显示大小", height);

            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(height));
            protoJsonStr = EditorGUILayout.TextArea(protoJsonStr, style);
            EditorGUILayout.EndScrollView();
            base.ApplyRevertGUI();
        }

        private string GetProtoBuffStatic(string protoType, byte[] data)
        {
            string jsonStr = null;
            if (ProtoBuffAssemblyRuntime.GetMessageTypeByAssembly(protoType, out IMessage message,
                    out Type meesageType))
            {
                message = message.Descriptor.Parser.ParseFrom(data);
                // 创建自定义的 JsonFormatter.Settings
                var settings = new JsonFormatter.Settings(
                    formatDefaultValues: true, // 是否包含默认值
                    typeRegistry: null
                );
                settings = settings.WithIndentation("   ");
                // 使用自定义设置创建 JsonFormatter
                var formatter = new JsonFormatter(settings);

                // 将 protobuf 消息转换为格式化的 JSON
                jsonStr = formatter.Format(message);
            }
            else
            {
                ProtoDebug.LogError($"找不到ProtoType:{protoType}");
            }

            return jsonStr;
        }


        public override void OnDisable()
        {
            base.OnDisable();
            hash = 0;
            protoTypeStr = null;
        }
    }
}